package com.lgd.factoryattendance.Activity.editActivity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lgd.factoryattendance.Activity.HomePageActivity;
import com.lgd.factoryattendance.Bean.DebugLog;
import com.lgd.factoryattendance.Bean.WorkHourStatistics;
import com.lgd.factoryattendance.R;
import com.lgd.factoryattendance.Utils.StringUtil;
import com.lgd.factoryattendance.Utils.ToastUtils;
import com.lgd.factoryattendance.httpService.API;
import com.lgd.factoryattendance.httpService.httpPort.IStringResponse;

import java.util.List;

public class EditDebugActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "EditDebugActivity";
    TextView tv_clock_type, tv_staff_name, tv_start_time, tv_end_time, tv_actual_time, tv_project_name;
    EditText et_debug_content;
    LinearLayout layout_close;
    ImageView iv_refresh;
    Long edit_work_id, debugLogID;
    Button bt_submit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_debug);
        initView();
        initData();
    }
    public void initData (){
        Intent intent = getIntent();
        edit_work_id = intent.getLongExtra("editWorkId",0);
        if (edit_work_id == 0){
            ToastUtils.warnShowLong(getApplicationContext(),"请刷新界面");
        }else {
            setDebugInfo(edit_work_id);
        }

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.layout_close:
                finish();
                break;
            case R.id.iv_refresh:
                reStartActivity();
                break;
            case R.id.bt_submit:
                String str_debug_content = et_debug_content.getText().toString();
                if (str_debug_content.equals("")){
                    ToastUtils.warnShowLong(getApplicationContext(), "调试内容不为空");
                }else {
                    inquireDialog(str_debug_content);
                }
                break;
            default:
                break;
        }
    }
    public void setDebugInfo (Long id){
        DebugLog debugLog = new DebugLog();
        debugLog.setWorkHourId(id);
        selectDebugLog(debugLog);
    }
    //  查询 最新 日志
    String str_extract;
    public void selectDebugLog (DebugLog debugLog){
        debugLogID = null;
        API.selectDebugLog(debugLog, new IStringResponse() {
            @Override
            public void onSuccess(String result) {
                EditDebugActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        JSONObject j = JSONObject.parseObject(result);
                        List<DebugLog> debugLogList = new Gson().fromJson(j.getString("data"), new TypeToken<List<DebugLog>>(){}.getType() );
                        System.out.println(debugLogList+"---日志shuju----");
                        if (debugLogList.size() > 0 ){
                            tv_clock_type.setText(debugLogList.get(0).getDebugType());
                            tv_staff_name.setText(debugLogList.get(0).getNickName());
                            tv_actual_time.setText(debugLogList.get(0).getDebugDuration());
                            tv_project_name.setText(debugLogList.get(0).getProjectname() + " ( " +
                                    debugLogList.get(0).getProductacode()+ " )");

                            String str_content = debugLogList.get(0).getDebugcontent();
                            str_extract = StringUtil.extractContentInBrackets(str_content);
                            System.out.println("--- 提取的内容 ---"+str_extract);

                            et_debug_content.setHint(debugLogList.get(0).getDebugcontent());

                            debugLogID = debugLogList.get(0).getDebuglogid();
                        }
                    }
                });
            }
            @Override
            public void onFail(int statusCode) {}
        });
    }
    //  修改 调试 日志
    public void updaterDebugLog (DebugLog debugLog){
        API.updaterDebugLog(debugLog, new IStringResponse() {
            @Override
            public void onSuccess(String result) {
                EditDebugActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        JSONObject j = JSONObject.parseObject(result);
                        long l = Long.parseLong(j.getString("code"));
                        if (l == 200){
                            ToastUtils.successShowLong(getApplicationContext(),"调试信息修改成功！");
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    //  返回首页
                                    Intent intent = new Intent(EditDebugActivity.this, HomePageActivity.class);
                                    setResult(Activity.RESULT_OK,intent);
                                    finish();
                                }
                            },200);
                        }
                    }
                });
            }
            @Override
            public void onFail(int statusCode) {}
        });
    }

    //    询问弹窗
    public void inquireDialog(String debug_content){
        AlertDialog alertDialog = new AlertDialog.Builder(this)
            .setTitle(" 修 改 提 醒")
            .setMessage("确定修改这条调试信息吗？")
            .setIcon(R.drawable.tishi)
            .setPositiveButton("确定", new DialogInterface.OnClickListener() {   //添加"Yes"按钮
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    DebugLog debugLog = new DebugLog();
                    debugLog.setDebuglogid(debugLogID);
                    debugLog.setDebugcontent("("+ str_extract +") "+debug_content);
                    updaterDebugLog(debugLog);
                }
            })
            .setNegativeButton("取消", new DialogInterface.OnClickListener() {//添加取消
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    ToastUtils.successShowLong(getApplicationContext(),"已取消");
                }
            })
            .create();
        alertDialog.show();
    }

    private void reStartActivity() {      // 刷新 当前Activity
        Intent intent = getIntent();
        overridePendingTransition(0, 0);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        finish();
        overridePendingTransition(0, 0);
        startActivity(intent);
    }
    public void initView (){
        bt_submit = findViewById(R.id.bt_submit);
        bt_submit.setOnClickListener(this);
        iv_refresh = findViewById(R.id.iv_refresh);
        iv_refresh.setOnClickListener(this);
        layout_close = findViewById(R.id.layout_close);
        layout_close.setOnClickListener(this);
        et_debug_content = findViewById(R.id.et_debug_content);
        tv_project_name = findViewById(R.id.tv_project_name);
        tv_actual_time = findViewById(R.id.tv_actual_time);
        tv_end_time = findViewById(R.id.tv_end_time);
        tv_start_time = findViewById(R.id.tv_start_time);
        tv_staff_name = findViewById(R.id.tv_staff_name);
        tv_clock_type = findViewById(R.id.tv_clock_type);
    }
}